import 'package:enterprise_v6_app/app/components/custom_modele/custom_progress_comp_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_home_page/general_view_all_page/general_view_all_controller.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

/// 安全生产作业-首页-总览
class GeneralViewAllPage extends StatelessWidget {
  GeneralViewAllPage({super.key});

  final controller = Get.put(GeneralViewAllController());

  /// 当前作业
  Widget _currentJob(String title, String count, String icon, Color colors) {
    return Expanded(
      child: Container(
        height: 64.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        decoration: BoxDecoration(
          color: colors.withOpacity(0.1),
          borderRadius: const BorderRadius.all(Radius.circular(8)),
        ),
        child: Row(
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  count,
                  style: TextStyle(
                    color: colors,
                    fontWeight: FontWeight.w500,
                    fontSize: 18.sp,
                  ),
                ),
                Text(
                  title,
                  style: TextStyle(
                    color: colors,
                    fontWeight: FontWeight.w500,
                    fontSize: 12.sp,
                  ),
                )
              ],
            ),
            const Spacer(),
            SvgPicture.asset(icon)
          ],
        ),
      ),
    );
  }

  /// 作业单位
  Widget _jobUnit(String title, String count, String icon) {
    return Expanded(
      child: Container(
        height: 58.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        decoration: BoxDecoration(
          color: AppColors.font_color_blue.withOpacity(0.1),
          borderRadius: const BorderRadius.all(Radius.circular(8)),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                SvgPicture.asset(icon),
                const Spacer(),
                Text(
                  count,
                  style: TextStyle(
                    color: AppColors.font_color_blue,
                    fontWeight: FontWeight.w500,
                    fontSize: 16.sp,
                  ),
                ),
              ],
            ),
            Text(
              title,
              style: TextStyle(
                color: const Color(0xffBEC5D4),
                fontSize: 12.sp,
              ),
            )
          ],
        ),
      ),
    );
  }

  ///获取历史概况标题
  Widget _getJobSituationTitle(String value) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 17.w, vertical: 12.w),
      child: Row(
        children: [
          SvgPicture.asset(
            AppImages.icon_title,
            width: 20.w,
            height: 20.w,
          ),
          AppGaps.hGap6,
          Text(
            '特殊作业历史概况',
            style: TextStyle(
                color: const Color(0xff262F38),
                height: 1.2,
                fontWeight: FontWeight.w500,
                fontSize: 14.sp),
          ),
          Expanded(
              child: Text(
            '$value次',
            textAlign: TextAlign.right,
            style: TextStyle(
                color: const Color(0xff5C6DFF),
                height: 1.2,
                fontWeight: FontWeight.w500,
                fontSize: 15.sp),
          ))
        ],
      ),
    );
  }

  /// 特殊作业历史概况
  Widget _specialJobSituation() {
    return Wrap(
      runSpacing: 12.w,
      spacing: 20.w,
      children: controller.stringData.map((e) {
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              width: 10.w,
              height: 10.w,
              decoration: BoxDecoration(
                color: e['color'] as Color,
                borderRadius: BorderRadius.circular(2),
              ),
            ),
            AppGaps.hGap8,
            Text(
              e['name'].toString(),
              style: TextStyle(
                color: AppColors.font_color_gray,
                fontSize: 12.sp,
              ),
            ),
          ],
        );
      }).toList(),
    );
  }

  /// 特殊作业历史
  Widget _specialJobHistory() {
    return Wrap(
      runSpacing: 12.w,
      spacing: 12.w,
      children: controller.stringData.map((e) {
        return Container(
          width: 52.w,
          alignment: Alignment.center,
          padding: EdgeInsets.symmetric(vertical: 3.w),
          decoration: BoxDecoration(
            color: e['color'] as Color,
            borderRadius: BorderRadius.circular(4),
          ),
          child: Text('${e['num']}',
              style: TextStyle(
                color: Colors.white,
                fontSize: 14.sp,
              )),
        );
      }).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return ListView(
          children: [
            Column(
              children: [
                AppGaps.vGap14,
                CustomTitleChartPage(
                  title: '当前作业',
                  child: Row(
                    children: [
                      _currentJob('总数', '${controller.currentJob.workingCount}',
                          AppImages.icon_job_count, const Color(0xff5C6DFF)),
                      AppGaps.hGap12,
                      _currentJob(
                          '预警违章',
                          '${controller.currentJob.violationCount}',
                          AppImages.icon_job_warning,
                          const Color(0xffFD6B6B)),
                    ],
                  ),
                ),
                AppGaps.vGap20,
                CustomTitleChartPage(
                  title: '作业单位违章概况',
                  child: Column(
                    children: [
                      Row(
                        children: [
                          _jobUnit(
                              '内部单位作业数',
                              '${controller.unitViolation.innerWorkCount}',
                              AppImages.icon_overview_unit),
                          AppGaps.hGap10,
                          _jobUnit(
                              '承包商单位数',
                              '${controller.unitViolation.contractorCount}',
                              AppImages.icon_overview_contractor),
                        ],
                      ),
                      AppGaps.vGap10,
                      Row(
                        children: [
                          _jobUnit(
                              '违章次数',
                              '${controller.unitViolation.innerViolationCount}',
                              AppImages.icon_overview_violation),
                          AppGaps.hGap10,
                          _jobUnit(
                              '违章单位数',
                              '${controller.unitViolation.contractorViolationCount}',
                              AppImages.icon_overview_violation_unit),
                        ],
                      ),
                    ],
                  ),
                ),
                AppGaps.vGap20,
                CustomTitleChartPage(
                  title: '任务板',
                  child: SizedBox(
                    height: 140.w,
                    child: SfCircularChart(
                      legend: Legend(
                        isVisible: true,
                        position: LegendPosition.right,
                        overflowMode: LegendItemOverflowMode.none,
                        legendItemBuilder: (String name, dynamic series,
                            dynamic point, int seriesIndex) {
                          return Container(
                            width: 100.w,
                            height: 22.w,
                            alignment: Alignment.center,
                            // margin: EdgeInsets.only(top: 11.w),
                            child: Row(
                              children: [
                                Container(
                                  width: 10.w,
                                  height: 10.w,
                                  margin: EdgeInsets.only(right: 5.w),
                                  padding: EdgeInsets.all(2.w),
                                  decoration: BoxDecoration(
                                      color: controller
                                          .questionData[seriesIndex].color,
                                      borderRadius: BorderRadius.circular(5.w)),
                                  child: Container(
                                    decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius:
                                            BorderRadius.circular(5.w)),
                                  ),
                                ),
                                Text(
                                  name,
                                  style: TextStyle(
                                      fontSize: 12.w,
                                      fontWeight: FontWeight.w400,
                                      color: const Color(0xffA0A7B5)),
                                ),
                                const Spacer(),
                                Text(
                                  '${controller.questionData[seriesIndex].y.toInt()}',
                                  style: TextStyle(
                                    fontSize: 12.sp,
                                    fontWeight: FontWeight.w400,
                                    color: controller
                                        .questionData[seriesIndex].color,
                                  ),
                                ),
                              ],
                            ),
                          );
                        },
                        toggleSeriesVisibility: false,
                      ),
                      annotations: [
                        CircularChartAnnotation(
                          widget: Container(
                            width: 56.w,
                            height: 56.w,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(50.w),
                            ),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  '总数',
                                  style: TextStyle(
                                    fontSize: 12.w,
                                    fontWeight: FontWeight.w400,
                                    color: const Color(0xffA0A7B5),
                                  ),
                                ),
                                Text(
                                  '${controller.taskTotal}',
                                  style: TextStyle(
                                      fontSize: 16.w,
                                      fontWeight: FontWeight.w400,
                                      color: const Color(0xFF495061)),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ],
                      series: <CircularSeries>[
                        DoughnutSeries<ChartData, String>(
                          dataSource: controller.questionData,
                          pointColorMapper: (ChartData data, _) => data.color,
                          xValueMapper: (ChartData data, _) => data.x,
                          yValueMapper: (ChartData data, _) => data.y,
                          name: '园区资料文件',
                          dataLabelSettings: const DataLabelSettings(
                            isVisible: false,
                            labelPosition: ChartDataLabelPosition.inside,
                          ),
                          radius: '100%',
                          innerRadius: '65%',
                          enableTooltip: true,
                        ),
                      ],
                    ),
                  ),
                ),
                AppGaps.vGap20,
                CustomTitleChartPage(
                  title: '作业异常',
                  child: SizedBox(
                    height: 230.w,
                    child: ListView.builder(
                      itemCount: controller.workHistoryList.length,
                      // shrinkWrap: true,
                      // physics: const NeverScrollableScrollPhysics(),
                      itemBuilder: (BuildContext context, int index) {
                        return CustomProgressComp(
                            bgColor: const Color(0xffEFF7FF),
                            height: 6.w,
                            width: 319.w,
                            value: controller.workHistoryList[index].value,
                            title:
                            controller.workHistoryList[index].ticketTypeName!,
                            num: controller.workHistoryList[index].ticketCount!
                                .toString());
                      },
                    ),
                  ),
                ),
                AppGaps.vGap20,
                CustomTitleChartPage(
                  titleView: _getJobSituationTitle(controller.historyTotal),
                  child: Column(
                    children: [
                      _specialJobSituation(),
                      AppGaps.vGap18,
                      _specialJobHistory(),
                    ],
                  ),
                ),
                AppGaps.vGap20,
              ],
            ),
          ],
        );
      },
    );
  }
}
